/**
 * 
 */
package org.youisoft.summer.evaluator.context;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.youisoft.lifecircle.Destroyable;
import org.youisoft.summer.evaluator.operator.Operator;

/**
 * @author zijiang.jl
 *
 */
public class CalContext<C,R,P> implements java.util.Map<String,Object>,java.lang.Cloneable,Destroyable{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String id;
	private boolean needIngore;
	
	private boolean needInterrupt;
	private boolean needSleep;
	private long sleepTime;
	
	private boolean needPause;
	private long pauseTime;
	private boolean needBreak;
	private boolean needContinue;
	
	private boolean needReInit;
	private boolean needStart;
	private boolean needReStart;
	private boolean needStop;

	private boolean isSynchronized=true;
	
	private boolean hasPayload;
	
	private String parenthesisClassName;
	private String braceClassName;
	private String bracketClassName;
	private String evaluatorClassName;
	private ContextCallback<R,P> callback;
	
	private C context;
	private R resource;
	private P property;
	private java.lang.Throwable throwable;
	private java.util.Map<String,Object> parameters=new java.util.concurrent.ConcurrentHashMap<String,Object>();
	
	public CalContext(){
		this.id=java.util.UUID.randomUUID().toString();
	}

	
	
	public final boolean hasPayload() {
		return hasPayload;
	}



	public final void setPayload(boolean hasPayload) {
		this.hasPayload = hasPayload;
	}



	public final boolean isNeedPause() {
		return needPause;
	}



	public final void setNeedPause(boolean needPause) {
		this.needPause = needPause;
	}



	public final long getPauseTime() {
		return pauseTime;
	}



	public final void setPauseTime(long pauseTime) {
		this.pauseTime = pauseTime;
	}



	public final boolean isNeedBreak() {
		return needBreak;
	}



	public final void setNeedBreak(boolean needBreak) {
		this.needBreak = needBreak;
	}



	public final boolean isNeedContinue() {
		return needContinue;
	}



	public final void setNeedContinue(boolean needContinue) {
		this.needContinue = needContinue;
	}



	public final boolean isSynchronized() {
		return isSynchronized;
	}



	public final void setSynchronized(boolean isSynchronized) {
		this.isSynchronized = isSynchronized;
	}



	public final String getId() {
		return id;
	}

	public final void setId(String id) {
		this.id = id;
	}

	public final boolean isNeedStart() {
		return needStart;
	}

	public final void setNeedStart(boolean needStart) {
		this.needStart = needStart;
	}

	public final boolean isNeedStop() {
		return needStop;
	}

	public final void setNeedStop(boolean needStop) {
		this.needStop = needStop;
	}

	
	
	
	public final boolean isNeedReInit() {
		return needReInit;
	}

	public final void setNeedReInit(boolean needReInit) {
		this.needReInit = needReInit;
	}

	public final boolean isNeedReStart() {
		return needReStart;
	}

	public final void setNeedReStart(boolean needReStart) {
		this.needReStart = needReStart;
	}

	public final boolean isNeedIngore() {
		return needIngore;
	}

	public final void setNeedIngore(boolean needIngore) {
		this.needIngore = needIngore;
	}

	public final boolean isNeedInterrupt() {
		return needInterrupt;
	}

	public final void setNeedInterrupt(boolean needInterrupt) {
		this.needInterrupt = needInterrupt;
	}
	
	

	public final boolean isNeedSleep() {
		return needSleep;
	}

	public final void setNeedSleep(boolean needSleep) {
		this.needSleep = needSleep;
	}

	public final long getSleepTime() {
		return sleepTime;
	}

	public final void setSleepTime(long sleepTime) {
		this.sleepTime = sleepTime;
	}

	public final R getResource() {
		return resource;
	}

	public final void setResource(R resource) {
		this.resource = resource;
	}

	public final P getProperty() {
		return property;
	}

	public final void setProperty(P property) {
		this.property = property;
	}

	
	
	public final String getBracketClassName() {
		return bracketClassName;
	}



	public final void setBracketClassName(String bracketClassName) {
		this.bracketClassName = bracketClassName;
	}



	public final String getParenthesisClassName() {
		return parenthesisClassName;
	}

	public final void setParenthesisClassName(String parenthesisClassName) {
		this.parenthesisClassName = parenthesisClassName;
	}
	
	
	
	public final String getBraceClassName() {
		return braceClassName;
	}

	public final void setBraceClassName(String braceClassName) {
		this.braceClassName = braceClassName;
	}
	
	

	public final String getEvaluatorClassName() {
		return evaluatorClassName;
	}

	public final void setEvaluatorClassName(String evaluatorClassName) {
		this.evaluatorClassName = evaluatorClassName;
	}

	public final ContextCallback<R, P> getCallback() {
		return callback;
	}

	public final void setCallback(ContextCallback<R, P> callback) {
		this.callback = callback;
	}

	public final C getContext() {
		return context;
	}

	public final void setContext(C context) {
		this.context = context;
	}

	public Operator<?> callback(ContextCallback<R,P> callback,String var,R resource,P property){
		return callback.call(var,resource, property);
	}
	
	public final java.lang.Throwable getThrowable() {
		return throwable;
	}

	public final void setThrowable(java.lang.Throwable throwable) {
		this.throwable = throwable;
	}
	
	
	public final java.util.Map<String, Object> getParameters() {
		return parameters;
	}



	public final void setParameters(java.util.Map<String, Object> parameters) {
		this.parameters = parameters;
	}



	@Override
	public void destory() {
		this.context=null;
		this.resource=null;
		this.parameters.clear();
		this.property=null;
		this.throwable=null;
	}



	@Override
	public CalContext<C,R,P> clone() throws CloneNotSupportedException {
		CalContext<C,R,P> cc=new CalContext<C,R,P>();
		cc.setEvaluatorClassName(this.getEvaluatorClassName());
		cc.setParenthesisClassName(this.getParenthesisClassName());
		cc.setBraceClassName(this.getBraceClassName());
		cc.setBracketClassName(this.getBracketClassName());
		cc.setCallback(this.getCallback());
		cc.setContext(this.getContext());
		cc.setResource(this.getResource());
		cc.setProperty(this.getProperty());
		cc.setParameters(this.getParameters());
		
		//===============
		cc.setSleepTime(this.getSleepTime());
		cc.setPauseTime(this.getPauseTime());
		
		return cc;
	}

	//=============================
	@Override
	public int size() {
		
		return this.parameters.size();
	}

	@Override
	public boolean isEmpty() {
		
		return this.parameters.isEmpty();
	}

	@Override
	public boolean containsKey(Object key) {
		
		return this.parameters.containsKey(key);
	}

	@Override
	public boolean containsValue(Object value) {
		
		return this.parameters.containsValue(value);
	}

	@Override
	public Object get(Object key) {
		
		return this.parameters.get(key);
	}

	@Override
	public Object put(String key, Object value) {
		
		return this.parameters.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		
		return this.parameters.remove(key);
	}

	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		
		this.parameters.putAll(m);
	}

	@Override
	public void clear() {
		this.parameters.clear();
		
	}

	@Override
	public Set<String> keySet() {
		
		return this.parameters.keySet();
	}

	@Override
	public Collection<Object> values() {
		
		return this.parameters.values();
	}

	@Override
	public Set<java.util.Map.Entry<String, Object>> entrySet() {
		return this.parameters.entrySet();
	}
	
}
